package first.group.ssh.packet;

import first.group.ssh.SSHConsts;
import first.group.ssh.globalstate.GlobalState;
import java.nio.ByteBuffer;

public class GlobalRequestSuccessPacket extends Packet {
    
    public final byte[] payload;
    public final GlobalState.SuccessPacket<? extends GlobalState> packet;

    public GlobalRequestSuccessPacket(byte[] payload) {
        super(SSHConsts.SSH_MSG_REQUEST_SUCCESS);
        this.payload = payload;
        this.packet = null;
    }

    public GlobalRequestSuccessPacket(GlobalState.SuccessPacket<? extends GlobalState> packet) {
        super(SSHConsts.SSH_MSG_REQUEST_SUCCESS);
        this.packet = packet;
        this.payload = null;
    }

    public static PacketParser<GlobalRequestSuccessPacket> PARSER = new Parser();

    private static class Parser extends PacketParser<GlobalRequestSuccessPacket> {

        @Override
        public GlobalRequestSuccessPacket parse(ByteBuffer buffer) {
            var payload = new byte[buffer.remaining()];
            buffer.get(payload);
            return new GlobalRequestSuccessPacket(payload);
        }
    }

    @Override
    public void write(ByteBuffer buffer) {
        if (packet == null) {
            buffer.put(payload);
        } else {
            packet.write(buffer);
        }
    }
}
